/** 
 * 
 * 获取年月日时分秒
 */
function formatTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}
// 获取月日 时分
function start_time(number) {
    let date=new Date(number * 1000)
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return month+"月"+day+"日 "+ formatNumber(hour)+":"+formatNumber(minute);
}

function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
}

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
 */
function formatTimeTwo(number, format) {
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];

    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));

    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));

    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}
// isdate 10位数时间戳 返回倒计时时分秒
function getcount(isdate) {
    if((isdate * 1000)-Date.parse(new Date())>0){
        var date = new Date((isdate * 1000)-Date.parse(new Date()));
        let differ_day =Math.floor(Math.floor(date/(24*3600*1000)))
        let differ_hour =Math.floor(date % (3600 * 1e3 * 24) / (1e3 * 60 * 60))
        let differ_minute = Math.floor(date % (3600 * 1e3) / (1000 * 60));
        let s = Math.floor(date % (3600 * 1e3) % (1000 * 60) / 1000);
        if(differ_day>0){
            return differ_day+"天 "+formatNumber(differ_hour)+":"+formatNumber(differ_minute)+":"+formatNumber(s);
        }else{
            return formatNumber(differ_hour)+":"+formatNumber(differ_minute)+":"+formatNumber(s);
        }
    }else{
        return 0
    }
}
// isdate 10位数时间戳 返回时分秒
function gettiming(isdate) {
    var date = new Date(isdate * 1000);
    
    return formatNumber(date.getHours())+":"+formatNumber(date.getMinutes())+":"+formatNumber(date.getSeconds());
}
// 获取当前年月日
function getToday() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
        month = '0' + month;
    };
    if (day < 10) {
        day = '0' + day;
    };
    var formatDate = year + '-' + month + '-' + day
    return formatDate;
}
function getHours(){
    return new Date(Date.parse(new Date())).getHours();
}
function getDateStr(today, addDayCount) {
    var date;
    if (today) {
        date = new Date(today);
    } else {
        date = new Date();
    }
    date.setDate(date.getDate() + addDayCount);//获取AddDayCount天后的日期 
    var y = date.getFullYear();
    var m = date.getMonth() + 1;//获取当前月份的日期 
    var d = date.getDate();
    if (m < 10) {
        m = '0' + m;
    };
    if (d < 10) {
        d = '0' + d;
    };
    return y + "-" + m + "-" + d;
}

module.exports.getToday = getToday
exports.start_time = start_time
exports.formatTime = formatTime
exports.getHours = getHours
exports.formatTimeTwo = formatTimeTwo
exports.getcount = getcount
exports.gettiming = gettiming
exports.getDateStr = getDateStr
